layui.define(["table", "form", "textSearch"], function(e) {
    var $ = layui.$,
        form = layui.form,
        table = layui.table;
    //search
    form.on('submit(LAY-front-search)', function(data) {
        var field = data.field;
        $(".layui-table-main").textSearch(field.ac_name);
        return false;
    });
    //表格范围外事件
    var active = {
        batchdel: function() {
            var checkStatus = table.checkStatus('LAY-manage'),
                checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            layer.confirm('确定删除吗？', function(index) {
                //执行 Ajax 后重载
                var _ids = layui.getParamValues(checkData, 'ac_id');
                $.delete(layui.setter.app_url + 'access/delete', {
                    id: _ids.join()
                }, function(data) {
                    layer.msg(data.msg);
                    table.reload('LAY-manage');
                });
            });
        },
        add: function() {
            var pid = parseInt($('.layui-btn.layuiadmin-btn-access').eq(2).data("ac_id"), 10);
            layer.open({
                type: 2,
                title: '添加权限',
                content: layui.setter.app_url + 'access/create?pid=' + pid,
                maxmin: true,
                area: ['600px', '450px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submitID = 'LAY-front-submit',
                        submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
                        var field = data.field;
                        $.post(layui.setter.app_url + 'access/save', field, function(data, textStatus, xhr) {
                            layer.msg(data.msg);
                            layer.close(index); //关闭弹层
                            table.reload('LAY-manage');
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
    };
    $('.layui-btn.layuiadmin-btn-access').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //表格内数据请求及动作 
    table.render({
            elem: "#LAY-manage",
            url: layui.setter.app_url + "access/listData",
            cols: [
                [{
                    type: "checkbox",
                    fixed: "left"
                }, {
                    field: "ac_id",
                    title: "ID",
                    width: 80,
                }, {
                    field: "ac_name",
                    title: "权限",
                    templet: function(d) {
                        return ('|一').repeat(d.level) + d.ac_name;
                    }
                }, {
                    field: "ac_url",
                    title: "网址",
                }, {
                    field: "ac_menu_flag_txt",
                    title: "菜单",
                }, {
                    field: "ac_order",
                    title: "排序",
                    edit: 'text',
                }, {
                    title: "操作",
                    with: 150,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-access-webuser"
                }]
            ],
            text: "对不起，加载出现异常！"
        }), table.on("tool(LAY-manage)", function(e) {
            if ("del" === e.event) {
                layer.confirm("真的删除行么", function(t) {
                    $.delete(layui.setter.app_url + 'access/delete?id=' + e.data.ac_id, function(data) {
                        layer.msg(data.msg), e.del();
                    });
                })
            } else if ("edit" === e.event) {
                var _data = e.data;
                layer.open({
                    type: 2,
                    title: "编辑权限",
                    content: app_url + "access/edit?id=" + _data.ac_id,
                    maxmin: !0,
                    area: ["600px", "450px"],
                    btn: ["确定", "取消"],
                    yes: function(e, t) {
                        var l = window["layui-layer-iframe" + e],
                            r = "LAY-front-submit",
                            n = t.find("iframe").contents().find("#" + r);
                        l.layui.form.on("submit(" + r + ")", function(t) {
                            $.put(layui.setter.app_url + 'access/update?id=' + _data.ac_id, t.field, function(data, textStatus, xhr) {
                                if (data.status == 1) {
                                    table.reload("LAY-manage"), layer.close(e)
                                }
                                layui.layer.msg(data.msg);
                            }, 'json');
                        }), n.trigger("click")
                    },
                    success: function(e, t) {}
                })
            }
        }), //监听单元格编辑
        table.on('edit(LAY-manage)', function(obj) {
            var value = obj.value,
                data = obj.data,
                field = obj.field;
            $.post('/admin/access/changeValue', {
                ac_id: data.ac_id,
                field: field,
                value: value
            }, function(data, textStatus, xhr) {
                layer.msg(data.msg);
            }, 'JSON');
        }), e("access", {})
});